/* 
 * Data storage class, abstracts file IO from other classes
 * An instance of this class should be declared in the System or main function
 * All other classes will take a pointer to this class when they are first created
 * 
 * Provides functions populate() and commit() to read in and write out data to files
 * Provides accounts,bookings and holidays containers.
 * Use object[] notation to access members
 */

#ifndef DATASTORE_H
#define	DATASTORE_H

#include "structs.h"
#include <algorithm>
#include <fstream>
#include <climits>
#include <sstream>
#include <cstdlib>

class tDataStore;

class DataStore {
    friend class tDataStore;
public:
    DataStore();
    virtual ~DataStore();
    // Clears data in database and rereads data from files
    void populate();
    // Call only at end of program. Writes back data to files
    void commit();

    // Given an ID, returns the i+1 of its index in accounts
    int findUserID(int);
    // Given a username, returns the i+1 of its index in accounts
    int findUserName(string);
    // Given a ref, returns the i+1 of its index in bookings
    int findBookRef(int);
    // Given a code, returns the i+1 of its index in holidays
    int findHolCode(int);

    // Given an ID, returns a deque of bookings related to the ID
    deque<bookStruct> listClientBookings(int);
    // Given a code, returns a deque of bookings with that holiday code
    deque<bookStruct> listHolidayBookings(int);

    // Simple function to see if a colon is present in a string input
    bool hasColon(string);
    // Returns a string of the type of account
    string accountString(accountType);
    // Returns a string of the type of holiday
    string holidayString(holidayType);
    // Returns a string of the status of the booking
    string bookingString(bookingStatus);

    // Data members, low level classes get data from these directly
    deque<accountStruct> accounts;
    deque<bookStruct> bookings;
    deque<holStruct> holidays;

    // Extension for mail
    deque<mail> mails;

    void removeHolidays(const int);
    void removeUser(const int);

private:
    // If you need clarifications of these functions ask me
    void readAccounts();
    void readBookings();
    void readHolidays();
    void readMail();

    int countLines();

    void writeAccounts();
    void writeBookings();
    void writeHolidays();
    void writeMail();

    string encryptDecrypt(string);
    void encode();
    void decode();

    fstream file;
};

#endif	/* DATASTORE_H */

